<?php
/*
 *  This file is part of TCDB.
 *  
 *  Copyright (C) 2000-2009
 *  Technology Consultant Corps
 *  Grinnell College
 *  Grinnell, IA 50112
 *  tc@grinnell.edu
 *
 *  TCDB is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  TCDB is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with TCDB; if not, see <http://www.gnu.org/licenses/>. 
 *
 
 ============================================================================

 * subscribe.php -- Generates an ics (Calendar) file that corresponds to a 
 *		    user's schedule, allowing the user to make his schedule
 *		    publicly available for use in other Calendar programs
 * 
 * Author: Elijah Buck
 * Created: ?
 * Last edited: 2009-07-12
 */

// Do the normal startup things.
require("init.php");

// Pull in the vCalendar class from Simian Systems' Sitellite (LGPL)
require("vcal/vCalendar.php");

/* Plan of action:
 * 1. Pick a time period and lab (or set of labs).
 * 2. Figure out what shifts fall into that selection.
 * 3. Walk through the list, adding these shifts to the calendar
 * 	as we go.
 * 4. Spit out the calendar.
 */

// Pick which parts of the schedule to produce

$start_date=date("Y-m-d",strtotime("-4 months"));
$end_date=date("Y-m-d",strtotime("+4 months"));
$user_id = (is_numeric($_GET["user_id"])) ? $_GET["user_id"] : "";

// prepare dates (likely unnecessary)
$start_date = strtotime($start_date);
$end_date = strtotime($end_date);

$start_date_date = date("Y-m-d",$start_date);
$end_date_date = date("Y-m-d",$end_date);

// Query for personal schedule
// We don't show those that don't want to publish schedules.
$query = sprintf("SELECT shifts.id,shifts.date,shifts.start_time,shifts.end_time,
			 users.first_name, users.last_name, users.email, users.id,
			 labs.name, ISNULL(shifts.sub_request_id)
		  FROM shifts, users, labs
		  WHERE shifts.lab_id = labs.id
		  AND users.schedule_p
		  AND shifts.user_id = users.id
		  AND date >= '$start_date_date'
		  AND date <= '$end_date_date'
		  AND shifts.user_id='%s'",
	      mysqli_real_escape_string($mysqli, $user_id));

$result = $mysqli->query($query, MYSQLI_STORE_RESULT);

// this step is very simple -- we just tell the class to build a calendar
// note that the class still requires us to know the vCal syntax
if ($result->num_rows > 0) { 
    $cal = new vCal();

    $cal->addProperty('VERSION','2.0');
    $cal->addProperty('PRODID','-//TCDB//TCDB vCalendar Generator//EN');
    $cal->addProperty('X-WR-TIMEZONE', 'America/Chicago');
    while(list($shift_id, $shift_date, $shift_start, $shift_end,
	       $user_fn, $user_ln, $user_email, $user_id, $lab_name, $sub_requested)
		    = $result->fetch_row()) {
	$shift_date_end = $shift_date;
	// first, we deal with the TCDB idiosyncracies
	$endpieces = explode(":", $shift_end);
	if($endpieces[0] >= 24) {
	    $endpieces[0] -= 24;
	    $shift_date_end = date("Y-m-d",strtotime("+1 day", strtotime($shift_date_end)));
	}
	$shift_end = implode(":", $endpieces);

	// now, fill in the description
	$description = "Shift for $user_fn $user_ln in $lab_name.";
	if ($sub_requested == 0) 
	  $description .= " Sub requested.";

	$event =& $cal->addEvent('VEVENT');

        $event->addProperty('X-TCDB-UID', $user_id);
        $event->addProperty('X-TCDB-SHIFTID', $shift_id);
        $event->addProperty('X-TCDB-SUBSTATUS', 0);
        $event->addProperty('DTSTART', date('Ymd\THis',
				strtotime("$shift_date $shift_start")));
        $event->addProperty('DTEND', date('Ymd\THis',
				strtotime("$shift_date_end $shift_end")));
        $event->addProperty ('SUMMARY', $lab_name);
        $event->addProperty ('LOCATION', $lab_name);
	$event->addProperty ('DESCRIPTION', $description);
    }
    // print the calendar
    header('Content-type: text/calendar');
    header('Content-Disposition: attachment; filename="TCDB.ics"');
    echo $cal->write();
}
?>
